home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / echsys10.zip / ENV_SET.ASM < prev    next >
Assembly Source File  |  1989-09-25  |  28KB  |  721 lines

  1.                  PAGE   60,132
  2.  
  3.                  ;Usage is: call envsub  ds:si -> length,string
  4.  
  5.                  ;      length is 1 byte long, <128
  6.                  ;         if high bit on, primary environment is set.
  7.                  ;      string is of form: name=value
  8.  
  9.                  ;   Copyright 1987, A. B. Krueger GPW MI 48236
  10.                  ;   All rights reserved. Contact "ARNY KRUEGER"
  11.                  ;   at the EXEC-PC BBS (414-964-5160) for permission
  12.                  ;   to use commercially.
  13.                  ;
  14.  
  15.                  ;Clone of SET command that demonstrates updating
  16.                  ;      the environment string.
  17.                  ;If there is no secondary command processor, the
  18.                  ;      global environment is updated
  19.                  ;If there is a secondary command processor, then
  20.                  ;      its enviroment is updated
  21.  
  22. sb               segment at 0      ;equates storage blocks and psp's
  23.  
  24. sb_kind          db     ' '        ;type of storage block: 'M' or 'Z'
  25. sb_psp           dw     ?          ;psp segment address
  26. sb_length        dw     ?          ;sb length in paragraphs
  27. sb_head_length   equ    10h        ;length of sb header
  28.                  org    sb_head_length
  29. sb_data          db     ?          ;data in block
  30.  
  31.                  org    0h         ;program segement prefix equates
  32. psp_ret_int      dw     ?          ;int 20h
  33.                  org    2Ch
  34. psp_env          dw     ?          ;segment address of environment
  35.                  org    50h
  36. psp_dos_function dw     ?          ;address of function dispatcher
  37.                  org    80h
  38. psp_parm_string  db     ?          ;1 byte length plus parm string
  39.  
  40. psp_length       equ    100h
  41.  
  42. sb               ends
  43.  
  44. cseg             segment para public
  45.                  assume cs:cseg,ds:cseg,es:sb
  46.                  public env_set
  47.  
  48.                  ;local  data
  49.  
  50. cr               equ    13
  51. lf               equ    10
  52.  
  53. sb_count         dw    0            ;count of sb's encountered
  54. sb_shell         dw    0            ;segment address of shell  sb
  55. sb_shell_env     dw    0            ;segment address of global env sb
  56. sb_secondary     dw    0            ;segment address of secondary command.com
  57. sb_secondary_env dw    0            ;segment address of secondary command env
  58.  
  59. fatal_msg       equ    80h
  60. error_msg       equ    40h
  61. info_msg        equ    20h
  62. msg_flag        db     fatal_msg+error_msg ;+info_msg   ;set flags
  63.                 db     'Copyright 1987, A. B. Krueger GPW MI 48236'
  64. secondary_msg   db     info_msg,'Secondary '
  65. command_found   db     info_msg,'COMMAND.COM found',cr,lf,'$'
  66. bad_dos_msg     db     fatal_msg,'Must be running under DOS 2.0 or above',cr,lf,'$'
  67. bad_sb_msg      db     fatal_msg,'Bad storage block',cr,lf,'$'
  68. bad_env_msg     db     error_msg,'Bad environment block',cr,lf,'$'
  69. command_lost    db     error_msg,'Shell never found',cr,lf,'$'
  70. addbadmsg       db     error_msg,'Environment corrupt',cr,lf,'$'
  71. addmsg          db     info_msg,'Addition requested',cr,lf,'$'
  72. removemsg       db     info_msg,'Removal requested',cr,lf,'$'
  73. env_set_nospace db     error_msg,'No space in environment string',cr,lf,'$'
  74. env_set_syntax  db     error_msg,'Set string syntax error',cr,lf,'$'
  75.  
  76. type_string     proc   near          ;type message at offset in dx
  77.                 push   ax            ;save registers
  78.                 push   cx
  79.                 push   dx
  80.                 push   si
  81.  
  82.                 mov    si,dx         ;get message level
  83.                 lodsb
  84.                 and    al,msg_flag   ;compare to what sells
  85.                 jz     type_ret      ;if not on list, send to bit bucket
  86.  
  87.                 mov    dx,si
  88.                 mov    ax,0900h
  89.                 int    21h
  90. type_ret:
  91.                 pop    si
  92.                 pop    dx
  93.                 pop    cx
  94.                 pop    ax
  95.                 ret
  96. type_string     endp
  97.  
  98. get_first_sb    proc   near       ;get first storage block, point es at it
  99.                 push   ax
  100.                 push   bx
  101.                 mov    ax,5200h
  102.                 int    21h        ;es:bx points to memory block anchor+2
  103.                 dec    bx
  104.                 dec    bx
  105.                 mov    es,es:[bx] ;get first memory block address into es
  106.                 pop    bx
  107.                 pop    ax
  108.                 ret
  109. get_first_sb    endp
  110.  
  111. get_next_sb     proc   near
  112.                 push   ax
  113.                 mov    ax,es             ;get current paragraph
  114.                 add    ax,sb_length      ;add in number of paragraphs
  115.                 inc    ax                ;add 1 for header
  116.                 mov    es,ax             ;set new extra segment address
  117.                 pop    ax
  118.                 ret
  119. get_next_sb     endp
  120.  
  121.  
  122. find_secondary_env proc  near       ;find env sb's for current program sb
  123.                 push   ax           ;pointed to by es
  124.                 push   es
  125.                 mov    ax,es        ;get address of secondary cp's sb
  126.                 inc    ax           ;get its psp address
  127. find_secondary_env_loop:
  128.                 call   get_next_sb  ;get next sb
  129.                 cmp    ax,sb_psp    ;match secondary's psp?
  130.                 jne    find_secondary_env_next    ;if not, skip
  131.  
  132.                 mov    sb_secondary_env,es        ;otherwise, save
  133.                 jmp    find_secondary_env_exit    ;and check no further
  134.                                                   ;lest we trash a .BAT block
  135. find_secondary_env_next:
  136.                 cmp    sb_kind,'Z'                ;last block?
  137.                 jne    find_secondary_env_loop
  138.  
  139. find_secondary_env_exit:
  140.                 pop    es
  141.                 pop    ax
  142.                 ret
  143. find_secondary_env endp
  144.  
  145. command_test    proc   near         ;test program storage block at es:0
  146.                 push   ax
  147.                 push   bx
  148.                 push   cx
  149.                 push   dx
  150.                 push   ds
  151.                 push   es
  152.                 push   si
  153.  
  154.                 cmp    sb_count,2
  155.                 ja     command_second
  156.  
  157.                 mov    dx,offset command_found
  158.                 call   type_string
  159.                 mov    sb_shell,es
  160.                 jmp    command_test_good
  161.  
  162. command_second:
  163.                 cmp    sb_shell,0                       ;did we find shell?
  164.                 je     command_first_bad                ;if not, error
  165.  
  166.                 cmp    word ptr es:psp_env+sb_head_length,0  ;check environment of program
  167.                 je     command_test_good                 ;if no environment, quit
  168.  
  169.                 push   sb_shell
  170.                 pop    ds                               ;ds points to shell
  171.                 mov    al,byte ptr es:sb_head_length+psp_length
  172.                 cmp    al,0E9h                          ;a JMP?
  173.                 jne    command_test_good                ;if not, no harm done
  174.  
  175.                 cmp    al,byte ptr ds:sb_head_length+psp_length   ;check 1st instruction
  176.                 jne    command_first_bad
  177.  
  178.                 mov    si,sb_head_length+psp_length
  179.                 mov    di,sb_head_length+psp_length
  180.                 mov    cx,10      ;look at 10 words of code
  181.                 repz   cmpsw
  182.                 clc
  183.                 jcxz   command_test_found   ;if they all match, fine
  184.  
  185.                 jmp    command_test_good    ;if not, no harm done
  186.  
  187. command_test_found:
  188.                 push   cs
  189.                 pop    ds
  190.                 mov    sb_secondary,es
  191.  
  192.                 mov    ax,es:psp_env+sb_head_length     ;get env address
  193.                 dec    ax                               ;back up over sb header
  194.                 mov    sb_secondary_env,ax              ;and save it
  195.  
  196.                 call   find_secondary_env               ;look for other env's
  197.  
  198.                 mov    dx,offset secondary_msg
  199.                 call   type_string
  200.                 jmp    command_test_good
  201.  
  202. command_first_bad:
  203.                 mov    dx,offset command_lost
  204.                 call   type_string
  205.                 stc
  206.                 jmp    c